package com.mall.user.services;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.ILoginService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.converter.MemberConverter;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dto.*;
import com.mall.user.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

//import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: DLX
 * @Date: 2021/6/20 18:08
 * @Description:
 **/
@Service
@Slf4j
public class LoginServiceImpl implements ILoginService {

    @Autowired
    MemberMapper memberMapper;

    @Autowired
    MemberConverter memberConverter;

    @Override
    public UserLoginResponse login(UserLoginRequest loginRequest) {
        UserLoginResponse response = new UserLoginResponse();
        // 参数校验
        loginRequest.requestCheck();
        // 登录验证
        String userName = loginRequest.getUserName();
        String userPwd = DigestUtils.md5DigestAsHex(loginRequest.getPassword().getBytes());

        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username", userName).andEqualTo("password", userPwd);
        List<Member> memberList = memberMapper.selectByExample(example);
        // 用户名或密码错误
        if (CollectionUtils.isEmpty(memberList)) {
            response.setCode(SysRetCodeConstants.WRONG_USERNAME_PASSWORD.getCode());
            response.setMsg(SysRetCodeConstants.WRONG_USERNAME_PASSWORD.getMessage());
            return response;
        }
        // 未激活
        Member member = memberList.get(0);
        if ("N".equals(member.getIsVerified())) {
            response.setCode(SysRetCodeConstants.USER_ISVERFIED_ERROR.getCode());
            response.setMsg(SysRetCodeConstants.USER_ISVERFIED_ERROR.getMessage());
            return response;
        }
        // 登录成功
        UserLoginResponse loginResponse = memberConverter.member2UserLoginResponse(member);
        log.info("登录成功,参数request:{}", JSON.toJSONString(loginRequest));
        // token
        Long uid = loginResponse.getId();
        String username = loginResponse.getUsername();

        HashMap<String, String> map = new HashMap<>();
        map.put("uid",uid.toString());
        map.put("username",userName);
        map.put("file",loginResponse.getFile());
//        String data = "{\"uid\":" + uid + ",\"username\":\"" + username + ",\"file\":\"" + loginResponse.getFile() +"\"}";
//        String data = loginResponse.getUsername() + loginResponse.getFile();// token根据什么生成
        String data = JSON.toJSONString(map);
        JwtTokenUtils tokenUtils = JwtTokenUtils.builder().msg(data).build();
        String token = tokenUtils.creatJwtToken();
        loginResponse.setToken(token);
        loginResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        loginResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return loginResponse;
    }

    @Override
    public CheckAuthResponse validToken(CheckAuthRequest checkAuthRequest) {
        checkAuthRequest.requestCheck();
        CheckAuthResponse checkAuthResponse = new CheckAuthResponse();
        String token = checkAuthRequest.getToken();
        JwtTokenUtils jwtTokenUtils = JwtTokenUtils.builder().token(token).build();
        String s = jwtTokenUtils.freeJwt();
        checkAuthResponse.setUserinfo(s);
        checkAuthResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        checkAuthResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return checkAuthResponse;
    }

    @Override
    public UserLoginVerifyResponse verifyLogin(String userInfo) {
        UserLoginVerifyResponse userLoginVerifyResponse = new UserLoginVerifyResponse();
        if (userInfo != null) {
            Map<String,String> map = JSON.parseObject(userInfo, Map.class);
            System.out.println(map);
            Long uid = Long.valueOf(map.get("uid"));
            System.out.println(uid);
            String username = map.get("username");
            String file = map.get("file");
//            String username = object.get("username").toString();
//            String file = object.get("file").toString();
            userLoginVerifyResponse.setUid(uid);
            userLoginVerifyResponse.setUsername(username);
            userLoginVerifyResponse.setFile(file);
//            return userLoginVerifyResponse;
        }
        return userLoginVerifyResponse;
    }


}
